package com.dao;

import com.domain.AdminBean;
import com.tools.DBUtil;

import java.security.spec.ECField;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class AdminDao {
    /**
     * 添加管理员信息
     * @param admin
     */
    public static void addAdmin(AdminBean admin) {
        Connection conn = null;
        PreparedStatement ps = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "insert into admin(loginId,loginPwd) values(?,?)";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, admin.getLoginId());
            ps.setString(2, admin.getLoginPwd());
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, ps, null);
        }
    }

    /**
     * 删除管理员信息
     * @param adminId
     */
    public static void deleteAdmin(int adminId) {
        Connection conn = null;
        PreparedStatement ps = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "delete from admin where loginId = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, adminId);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, ps, null);
        }
    }

    /**
     * 修改管理员信息
     * @param adminId
     * @param adminPwd
     */
    public static void updateAdminPassword(int adminId, String adminPwd) {
        Connection conn = null;
        PreparedStatement ps = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "update admin set loginPwd = ? where loginId = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, adminPwd);
            ps.setInt(2, adminId);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, ps, null);
        }
    }

    /**
     * 查看管理员信息
     * @return
     */
    public static List<AdminBean> selectAd() {
        List<AdminBean> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "select * from admin";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                int loginId = rs.getInt("loginId");
                String loginPwd = rs.getString("loginPwd");

                //将结果集的每行数据存进对象list中
                AdminBean a = new AdminBean(loginId,loginPwd);

                //将对象封装到List中
                list.add(a);
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.close(conn, ps, rs);
        }

        return null;
    }
}
